{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: multicolored line\n",
      "======================================================================\n",
      "\n",
      "Defining colors manually\n",
      "------------------------\n",
      "\n",
      "[colored_line.py](files/attachments/Matplotlib_MulticoloredLine/colored_line.py) is a simple illustration of how to make the\n",
      ")# color of each segment of a line depend on some property of the data\n",
      "being plotted.\n",
      "\n",
      "An up to date version of the script can be found\n",
      "[here](http://matplotlib.sourceforge.net/gallery.html).\n",
      "\n",
      "![](files/attachments/Matplotlib_MulticoloredLine/colored_line.png)\n",
      "\n",
      "Here is the script:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!/usr/bin/env python\n",
      "'''\n",
      "Color parts of a line based on its properties, e.g., slope.\n",
      "'''\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from matplotlib.collections import LineCollection\n",
      "from matplotlib.colors import ListedColormap, BoundaryNorm\n",
      "\n",
      "x = np.linspace(0, 3 * np.pi, 500)\n",
      "y = np.sin(x)\n",
      "z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative\n",
      "\n",
      "# Create a colormap for red, green and blue and a norm to color\n",
      "# f' < -0.5 red, f' > 0.5 blue, and the rest green\n",
      "cmap = ListedColormap(['r', 'g', 'b'])\n",
      "norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)\n",
      "\n",
      "# Create a set of line segments so that we can color them individually\n",
      "# This creates the points as a N x 1 x 2 array so that we can stack points\n",
      "# together easily to get the segments. The segments array for line collection\n",
      "# needs to be numlines x points per line x 2 (x and y)\n",
      "points = np.array([x, y]).T.reshape(-1, 1, 2)\n",
      "segments = np.concatenate([points[:-1], points[1:]], axis=1)\n",
      "\n",
      "# Create the line collection object, setting the colormapping parameters.\n",
      "# Have to set the actual values used for colormapping separately.\n",
      "lc = LineCollection(segments, cmap=cmap, norm=norm)\n",
      "lc.set_array(z)\n",
      "lc.set_linewidth(3)\n",
      "plt.gca().add_collection(lc)\n",
      "\n",
      "plt.xlim(x.min(), x.max())\n",
      "plt.ylim(-1.1, 1.1)\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note that the number of segments is one less than the number of points.\n",
      "\n",
      "An alternative strategy would be to generate one segment for each\n",
      "contiguous region of a given color.\n",
      "\n",
      "Using a smooth, builtin colormap\n",
      "--------------------------------\n",
      "\n",
      "If you have a parametric curve to display, and want to represent the\n",
      "parameter using color.\n",
      "\n",
      "![](files/attachments/Matplotlib_MulticoloredLine/colored_line2.png)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from matplotlib.collections import LineCollection\n",
      "\n",
      "t = np.linspace(0, 10, 200)\n",
      "x = np.cos(np.pi * t)\n",
      "y = np.sin(t)\n",
      "\n",
      "# Create a set of line segments so that we can color them individually\n",
      "# This creates the points as a N x 1 x 2 array so that we can stack points\n",
      "# together easily to get the segments. The segments array for line collection\n",
      "# needs to be numlines x points per line x 2 (x and y)\n",
      "points = np.array([x, y]).T.reshape(-1, 1, 2)\n",
      "segments = np.concatenate([points[:-1], points[1:]], axis=1)\n",
      "\n",
      "# Create the line collection object, setting the colormapping parameters.\n",
      "# Have to set the actual values used for colormapping separately.\n",
      "lc = LineCollection(segments, cmap=plt.get_cmap('copper'),\n",
      "    norm=plt.Normalize(0, 10))\n",
      "lc.set_array(t)\n",
      "lc.set_linewidth(3)\n",
      "\n",
      "plt.gca().add_collection(lc)\n",
      "plt.xlim(-1, 1)\n",
      "plt.ylim(-1, 1)\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
